<?php
    
namespace common;

class BloggDAL {
    private $m_postsTableName = "postsblogg";

    private $m_connection;
    
    public function __construct(\common\DbConnection $connection) {
            $this->m_connection = $connection;
    }
      
	/*//Lagrar den nya användaren i databasen
	public function insertUserDAL($user){
		//Om det är ledigt så skapas en ny användare
		$sql = "INSERT INTO $this->m_userTableName (userName, password) VALUES(?, ?)";
		$stmt = $this->m_connection->prepare($sql);
		$stmt->bind_param("ss", $user->userName, $user->password );
		//Pga unik index på username i db return stmt_execute false om användaren finns
		if($stmt->execute()==TRUE){ //utför en fråga från prepare
			$user->userID=$stmt->insert_id;
			return $user;	
		}
		return FALSE;
	}*/
    
   /* public function deleteUserDAL($m_user) {
        $sql = "DELETE FROM $this->m_userTableName WHERE userName = ?" ;
		$stmt = $this->m_connection->prepare($sql);
		$stmt->bind_param("s", $m_user);
		$stmt->execute();
		return $m_user;
    }*/
        
	/*public function getUserByUserNameDAL($username){
		$sql = "SELECT * FROM $this->m_userTableName WHERE userName = ?";
		$stmt = $this->m_connection->prepare($sql);
		$stmt->bind_param("s", $username);//"s" står för string | Binds varibles to a prepared statment as parameters
		//Genom att använda fetchObject hämtar man all data om en användare
		$user = $this->getUser($stmt);
		
		return $user;
	}*/
	
	/*	//Måtste ha dessa för att kunna använda webbhotellet
	public function getUser($stmt){
		$ret = array();
		
		$stmt->execute();
		
		$stmt->bind_result($userID, $userName, $password);
		
		while($stmt->fetch()){
			$user = new \Blogg\Handler\User();
			$user->userID = $userID;
			$user->userName = $userName;
			$user->password = $password;
		}
		return $user;
	}*/
	
	/**
	 * --------------------------------TEXT-------------------------------------
	 */
	      
	public function insertPostsDAL($posts, $datePosts, $title){
		$sql = "INSERT INTO  $this->m_postsTableName (posts, datePosts, title) VALUES(?,?,?)";
		$stmt = $this->m_connection->prepare($sql);
		$stmt->bind_param("sis", $posts, $datePosts, $title);
		if($stmt->execute()==TRUE){
			$posts=$stmt->insert_id;
			return $posts;	
		}
		return FALSE;
	}
	
    public function deletePostDAL($postID) {
        $sql = "DELETE FROM $this->m_postsTableName WHERE postsID = ?" ;
		$stmt = $this->m_connection->prepare($sql);
		$stmt->bind_param("i", $postID);
		$stmt->execute();
		return $postID;
    }
	
	public function updatePostDAL($postID){
		$sql = "UPDATE `$this->m_postsTableName` SET `posts`= ?,`datePosts`= ?,`title`= ? WHERE postsID = ?";
	 	$stmt = $this->m_connection->prepare($sql);
		$stmt->bind_param("sisi", $postID->posts, $postID->datePosts, $postID->title, $postID->postsID);
		$stmt->execute();
		return $postID;
	}
    
	//stjärnan!!!
    public function getPostsDAL(){
		$sql = "SELECT * FROM $this->m_postsTableName ORDER BY postsID DESC";
		$stmt = $this->m_connection->prepare($sql);
		$posts = $this->getPosts($stmt);
		return $posts;
	}
	
	public function getPostById($postsID){
		$sql = "SELECT * FROM $this->m_postsTableName WHERE postsID = ?";
		$stmt = $this->m_connection->prepare($sql);
		$stmt->bind_param("i", $postsID);
		$posts = $this->getPosts($stmt);
		return $posts;
	}
	
	public function getPosts($stmt){
		$stmt->execute();
		$stmt->bind_result($postsID, $posts, $datePosts, $title);
		
		while($stmt->fetch()){
			$post = new \common\Posts();
			$post->postsID = $postsID;
			$post->posts = $posts;
			$post->datePosts = $datePosts;
			$post->title = $title;
			
			$ret[] = $post;
		}
		return $ret;
	}
}
	
	/**
	 * -----------------------------------KOMMENTAR--------------------------------------
	 */
	
	/*public function insertCommentDAL($comment){
		$sql = "INSERT INTO  $this->m_commentTableName (comment) VALUES(?)";
		$stmt = $this->m_connection->prepare($sql);
		$stmt->bind_param("s", $comment);
		//Pga unik index på username i db return stmt_execute false om användaren finns
		if($stmt->execute()==TRUE){ //utför en fråga från prepare
			$comment=$stmt->insert_id;
			return $comment;	
		}
		return FALSE;
	}
    
    public function getCommentDAL(){
		$sql = "SELECT * FROM $this->m_commentTableName";
		$stmt = $this->m_connection->prepare($sql);
		$comment = $this->getComment($stmt);
		return $comment;
	}
	
	public function getComment($stmt){
		$ret = array();
		
		$stmt->execute();
		
		$stmt->bind_result($commentID, $comment);
		while($stmt->fetch()){
			$com = new Comment();
			$com->commentID = $commentID;
			$com->comment = $comment;
			
			$ret[] = $com;
		}
		return $ret;
	}	
}*/









/**
 * 	TODO: Needs PHP version 5.0
 */
/*	
	private function fetchTextFileObject(\mysqli_stmt $stmt){
		return $this->m_connection->fetchTextFileObject($stmt, "\Blogg\Handler\Text");
	}
	public function fetchTextFileObject(\mysqli_stmt $stmt, $Text){
		$ret = array();
		
		$stmt->execute();
		
		//$ret = $this->getText($stmt);
		$result = $stmt->get_result();
		//hämta ut alla objekt
		while($object = $result->fetch_object($Text)){
			$ret[] = $object;
		}
		
		$stmt->close();
		
		return $ret;
	}
 * 
 * 
 * 	private function fetchObject(\mysqli_stmt $stmt){
		return $this->m_connection->fetchObject($stmt, "\Blogg\Handler\User");
	}
 * 
 * 
 * 	private function fetchCommentObject(\mysqli_stmt $stmt){
		return $this->m_connection->fetchCommentObject($stmt, "\Blogg\Handler\Comment");
	}
 * 
 * 
 * 	public function fetchObject(\mysqli_stmt $stmt, $User){
		$ret = array();	
		$stmt->execute();
		$result = $stmt->get_result();
		$object = $result->fetch_object($User);
		//$ret = $this->getUser($stmt);
		$stmt->close();
		return $object;
	}*/
	/**
	 * 	END: Needs PHP version 5.0
	 */
